/*************************************************
* Title：Unity Shader Tutorial
* Author：Opportunity 
* Describe：卡通着色效果
* 轮廓线+渐变纹理
* CreateTime：2020-12-07 16:58:23
* Version：1.0
* Modify Recorder：
*************************************************/
Shader "Custom Shader/Toon Shading"
{
    Properties
    {
        _Color ("Tint Color",Color) = (1,1,1,1)
        _RampTex ("Ramp Texture",2D) = "white" {}
        
        [Space(5)]
        _OutlineColor ("Outline Color",Color) = (0,0,0,1)
        _Outline ("Outline",Range(0,1)) = 0.1

        [Space(5)]
        _Specular ("Specular",Color) = (1,1,1,1)
        _SpecularScale ("Specular Scale",Range(0,100)) = 0.01
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" "Queue"="Transparent"}

        //渲染轮廓线描边
        //采用过程式几何轮廓线渲染的方式，适用于绝大多数表面平滑的软边模型，但不适合类似立方体的平整硬边模型
        //第一个Pass负责渲染背面的面片，通过法线外拓的方式使得轮廓可见
        Pass
        {
            Cull Front
            Zwrite off
            
            CGPROGRAM
            
            #pragma vertex vert
            #pragma fragment frag
            
            #include "UnityCG.cginc"
            
            float _Outline;
            fixed4 _OutlineColor;
            
            struct a2v
            {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
            };
            
            struct v2f
            {
                float4 pos : SV_POSITION;
            };
            
            //为了让描边效果可以在观察空间中达到最好效果，在顶点着色器中把顶点和法线转换到视角空间
            //统一法线的z向量，归一化处理后再将顶点沿其方向扩张，是为了尽可能避免背面扩张后的顶点挡住正面的面片
            //最后，将顶点从视角空间转换为齐次裁剪空间
            v2f vert(a2v v)
            {
                v2f o;
                float4 pos = mul(UNITY_MATRIX_MV,v.vertex);
                float3 normal = mul((float3x3)UNITY_MATRIX_MV,v.normal);
                normal.z = -0.5;
                pos += float4(normalize(normal),0) * _Outline;
                o.pos = mul(UNITY_MATRIX_P,pos);
                
                return o;
            }
            
            float4 frag(v2f i):SV_Target
            {
                return float4(_OutlineColor.rgb,1);
            }
            
            ENDCG
        }
        
        //第二个Pass负责渲染正面的面片
        Pass
        {
            Tags { "LightMode"="ForwardBase" }
            
            cull Back
        
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"
            #include "Lighting.cginc"
            
            fixed4 _Color;
            sampler2D _RampTex;
            float4 _RampTex_ST;
            fixed4 _Specular;
            fixed _SpecularScale;

            struct a2v
            {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float4 pos : SV_POSITION;
                float2 uv : TEXCOORD0;
                float3 worldNormal : TEXCOORD1;
                float3 worldPos : TEXCOORD2;
            };

            v2f vert (a2v v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv,_RampTex);
                o.worldNormal = UnityObjectToWorldNormal(v.normal);
                o.worldPos = mul(unity_ObjectToWorld,v.vertex).xyz;
                
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                fixed3 worldNormal = normalize(i.worldNormal);
                fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
                fixed3 worldViewDir = normalize(UnityWorldSpaceViewDir(i.worldPos));
                fixed3 worldHalfDir = normalize(worldLightDir + worldViewDir);
                
                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
                fixed diff = dot(worldNormal,worldLightDir) * 0.5 + 0.5;
                fixed3 diffuse = _LightColor0.rgb * _Color.rgb * tex2D(_RampTex,float2(diff,0.5)).rgb;
               
                fixed spec = saturate(dot(worldNormal,worldHalfDir));
                fixed w = fwidth(spec) * 2;//计算出该像素与相邻两个像素的高光差值
                fixed3 specular = _Specular.rgb * lerp(0,1,smoothstep(-w,w,spec + _SpecularScale - 1)) * step(0.0001,_SpecularScale);
               
                fixed3 col = ambient + diffuse + specular;
                return float4(col,1);
            }
            ENDCG
        }
    }
    
    FallBack "Diffuse"
}
